﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraCtrl : MonoBehaviour
{
    [SerializeField] GameObject player;
    [SerializeField] float min_x, max_x;
    [SerializeField] float min_y, max_y;


    // Start is called before the first frame update
    void Start()
    {
        if (min_x > max_x)
        {
            Debug.LogError("CameraCtrl.cs min_x > max_x");
        }
        if (min_y >max_y)
        {
            Debug.LogError("CameraCtrl.cs min_y > max_y");
        }
    }

    private void FixedUpdate()
    {
        float nx, ny;
        nx = player.GetComponent<Behavior>().GetCenterPos().x;
        ny = player.GetComponent<Behavior>().GetCenterPos().y;

        nx = (nx > max_x) ? max_x : nx;
        nx = (nx < min_x) ? min_x : nx;

        ny = (ny > max_y) ? max_y : ny;
        ny = (ny < min_y) ? min_y : ny;

        SetPos_XY(nx, ny);
    }

    void SetPos_XY(float x, float y)
    {
        transform.position = new Vector3(x, y, transform.position.z);
    }
}
